﻿using RockBus.CacheItemConfiguration.Internal;
using System;

namespace RockBus.CacheItemConfiguration
{
    public interface ICacheItemConfiguration
    {
        string CacheItemName { get; }

        int TimeToLiveSecs { get; }
    }

    public class CacheItemConfiguration : ICacheItemConfiguration
    {
        private readonly string DefaultCacheItemName = Guid.NewGuid().ToString();
        private const int DefaultTimeToLive = -1;

        public CacheItemConfiguration(string key)
        {
            this.Key = key;
        }

        public string Key { get; set; }

        public string CacheItemName
        {
            get
            {
                if ((null != CacheItemConfigurationSection.Instance) &&
                    (null != CacheItemConfigurationSection.Instance.CacheItemConfigurations))
                {
                    var cic = CacheItemConfigurationSection.Instance.CacheItemConfigurations.GetItemByKey(this.Key);
                    if (null != cic)
                    {
                        return CacheItemConfigurationSection.Instance.CacheItemConfigurations[this.Key].CacheItemName;
                    }
                }
                return DefaultCacheItemName;
            }
        }

        public int TimeToLiveSecs// { get { return CacheItemConfigurationSection.Instance.CacheItemConfigurations[this.Key].TimeToLiveSecs; } }
        {
            get
            {
                if ((null != CacheItemConfigurationSection.Instance) &&
                    (null != CacheItemConfigurationSection.Instance.CacheItemConfigurations))
                {
                    var cic = CacheItemConfigurationSection.Instance.CacheItemConfigurations.GetItemByKey(this.Key);
                    if (null != cic)
                    {
                        return CacheItemConfigurationSection.Instance.CacheItemConfigurations[this.Key].TimeToLiveSecs;
                    }
                }
                return DefaultTimeToLive;
            }
        }
    }
}